﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using static WeatherComm.Log;

namespace WeatherComm
{
    public class FileHelper
    {
        /// <summary>
        /// 获取指定路径下所有文件
        /// </summary>        
        public static List<string> GetFiles(string path)
        {
            try
            {
                if (!Directory.Exists(path)) return null;
                var result = new List<string>();
                var dir = new DirectoryInfo(path);
                var subfolder = dir.GetDirectories();
                if (subfolder.Length > 0)
                {
                    foreach (var item in subfolder)
                    {
                        var subFiles = GetFiles(item.FullName);
                        if (subFiles != null && subFiles.Count > 0)
                            result.AddRange(subFiles);
                    }
                }
                var files = dir.GetFiles();
                result.AddRange(files.Select(file => file.FullName));
                return result;
            }
            catch (Exception e)
            {
                WriteLog(e.Message);
                return null;
            }
        }

        /// <summary>
        /// 获取指定文件内容
        /// 注：这里适用于读取文本类型文件
        /// </summary>
        public static List<string> GetTxtFileContent(string fileName, Encoding encoding = null)
        {
            try
            {
                if (!File.Exists(fileName)) return null;
                if (encoding == null)
                {
                    encoding = Encoding.Default;
                }
                var result = File.ReadAllLines(fileName, encoding);
                return result.ToList();
            }
            catch (Exception e)
            {
                WriteLog(e.Message);
                return null;
            }
        }
        public static string GetAllText(string fileName, Encoding encoding = null)
        {
            
            try
            {
                if (!File.Exists(fileName)) return null;
                if (encoding == null)
                {
                    encoding = Encoding.Default;
                }
                var result = File.ReadAllText(fileName, encoding);
                return result;
            }
            catch (Exception e)
            {
                WriteLog(e.Message);
                return null;
            }
        }
        /// <summary>
        /// 删除指定文件
        /// </summary>        
        public static void DeleteFile(string fileName)
        {
            try
            {
                File.Delete(fileName);
            }
            catch (Exception e)
            {
                WriteLog(e.Message);
            }
        }

        /// <summary>
        /// 创建文件
        /// </summary>
        /// <param name="fileName">文件全路径</param>
        /// <param name="contents">写入内容</param>
        /// <param name="encoding">编码</param>
        public static void CreateFile(string fileName, string[] contents = null, Encoding encoding = null)
        {
            if (encoding == null)
            {
                encoding = Encoding.Default;
            }
            try
            {
                if (contents != null && contents.Length > 0)
                {
                    File.WriteAllLines(fileName, contents);
                }
            }
            catch (Exception e)
            {
                WriteLog(e.Message);
            }
        }
        public static void CreateFile(string fileName, string value, Encoding encoding = null)
        {
            if (encoding == null)
            {
                encoding = Encoding.Default;
            }
            try
            {
                if (value != null )
                {
                    File.WriteAllText(fileName, value);
                }
            }
            catch (Exception e)
            {
                WriteLog(e.Message);
            }
        }
    }
}